#include "../include/admin_page.h"

static lv_ft_info_t info;
static lv_style_t style;

static lv_obj_t *admin_login_page;
static lv_obj_t *ta_username;
static lv_obj_t *lb_password;
static lv_obj_t *ta_password;
static lv_obj_t *kb;

static lv_obj_t *admin_home_page;
static lv_obj_t *btn_remove;
static lv_obj_t *btn_change;
static lv_obj_t *btn_search;

static lv_obj_t *admin_add_page;
static lv_obj_t *admin_remove_page;
static lv_obj_t *admin_find_page;
static lv_obj_t *admin_change_page;
static lv_obj_t *admin_search_page;

static lv_obj_t *ta_name;
static lv_obj_t *lb_quantity;
static lv_obj_t *ta_quantity;
static lv_obj_t *lb_price;
static lv_obj_t *ta_price;
static lv_obj_t *lb_image_name;
static lv_obj_t *ta_image_name;

static lv_obj_t *list_name;

enum btn_event ev;

static Node *curr_find_node;

void admin_page_init()
{
    info.name = "/font/simkai.ttf";
    info.weight = 20;
    info.style = FT_FONT_STYLE_NORMAL;
    info.mem = NULL;
    if(!lv_ft_font_init(&info)) {
        LV_LOG_ERROR("create failed.");
    }

    lv_style_init(&style);
    lv_style_set_text_font(&style, info.font);
}

void show_admin_login_page()
{
    admin_login_page = lv_obj_create(NULL);
    lv_obj_set_size(admin_login_page, 800, 480);
    lv_obj_set_style_pad_all(admin_login_page, 0, 0);

    lv_obj_add_event_cb(admin_login_page, admin_login_page_hide_kb_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *btn_ret = lv_btn_create(admin_login_page);
    lv_obj_set_size(btn_ret, 100, 50);
    lv_label_t *lb_ret = lv_label_create(btn_ret);
    lv_obj_add_style(lb_ret, &style, 0);
    lv_obj_center(lb_ret);
    lv_label_set_text(lb_ret, "返回");
    lv_obj_add_event_cb(btn_ret, admin_login_ret_outer_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *lb_title = lv_label_create(admin_login_page);
    lv_obj_align(lb_title, LV_ALIGN_TOP_MID, 0, 50);
    lv_obj_add_style(lb_title, &style, 0);
    lv_label_set_text(lb_title, "管理员登录");

    lv_obj_t *lb_username = lv_label_create(admin_login_page);
    lv_obj_set_pos(lb_username, 190, 160);
    lv_obj_set_size(lb_username, 100, 50);
    lv_obj_add_style(lb_username, &style, 0);
    lv_label_set_text(lb_username, "用户名");

    ta_username = lv_textarea_create(admin_login_page);
    lv_obj_align_to(ta_username, lb_username, LV_ALIGN_OUT_RIGHT_TOP, 5, -10);
    lv_obj_set_size(ta_username, 300, 50);
    lv_obj_add_style(ta_username, &style, 0);
    lv_textarea_set_one_line(ta_username, true);
    lv_textarea_set_placeholder_text(ta_username, "请输入用户名");

    lv_obj_add_event_cb(ta_username, ta_username_cb, LV_EVENT_CLICKED, NULL);

    lb_password = lv_label_create(admin_login_page);
    lv_obj_set_pos(lb_password, 190, 260);
    lv_obj_set_size(lb_password, 100, 50);
    lv_obj_add_style(lb_password, &style, 0);
    lv_label_set_text(lb_password, "密码");

    ta_password = lv_textarea_create(admin_login_page);
    lv_obj_align_to(ta_password, lb_password, LV_ALIGN_OUT_RIGHT_TOP, 5, -10);
    lv_obj_set_size(ta_password, 300, 50);
    lv_obj_add_style(ta_password, &style, 0);
    lv_textarea_set_one_line(ta_password, true);
    lv_textarea_set_password_mode(ta_password, true);
    lv_textarea_set_password_show_time(ta_password, 1000);
    lv_textarea_set_placeholder_text(ta_password, "请输入密码");

    lv_obj_add_event_cb(ta_password, ta_password_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *btn_login = lv_btn_create(admin_login_page);
    lv_obj_set_pos(btn_login, 300, 350);
    lv_obj_set_size(btn_login, 200, 50);

    lv_obj_t *lb_login = lv_label_create(btn_login);
    lv_obj_add_style(lb_login, &style, 0);
    lv_label_set_text(lb_login, "登录");
    lv_obj_center(lb_login);

    lv_obj_add_event_cb(btn_login, admin_login_cb, LV_EVENT_CLICKED, NULL);

    kb = lv_keyboard_create(admin_login_page);
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);

    lv_scr_load(admin_login_page);
}

void admin_login_page_hide_kb_cb(lv_event_t *e)
{
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
    lv_obj_set_pos(lb_password, 190, 260);
    lv_obj_align_to(ta_password, lb_password, LV_ALIGN_OUT_RIGHT_TOP, 5, -10);
}

void admin_page_hide_kb_cb(lv_event_t *e)
{
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
}

void admin_login_ret_outer_cb(lv_event_t *e)
{
    show_outer_page();
    lv_obj_del(admin_login_page);
}

void ta_username_cb(lv_event_t *e)
{
    lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
    lv_keyboard_set_textarea(kb, ta_username);
}

void ta_password_cb(lv_event_t *e)
{
    lv_obj_set_pos(lb_password, 190, 50);
    lv_obj_align_to(ta_password, lb_password, LV_ALIGN_OUT_RIGHT_TOP, 5, -10);
    lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
    lv_keyboard_set_textarea(kb, ta_password);
}

void admin_login_cb(lv_event_t *e)
{
    const char *username = lv_textarea_get_text(ta_username);
    const char *password = lv_textarea_get_text(ta_password);

    if(find_admin(username, password))
    {
        show_admin_home_page();
        lv_obj_del(admin_login_page);
    }
    else
    {
        static const char *btns[] = {""};
        lv_obj_t *msgbox = lv_msgbox_create(lv_scr_act(), "提示", "用户名或密码错误！", btns, true);
        lv_obj_t *title = lv_msgbox_get_title(msgbox);
        lv_obj_t *text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(title, &style, 0);
        lv_obj_add_style(text, &style, 0);
        lv_obj_center(msgbox);
    }
}

void show_admin_home_page()
{
    admin_home_page = lv_obj_create(NULL);
    lv_obj_set_size(admin_home_page, 800, 480);

    lv_obj_t *lb_title = lv_label_create(admin_home_page);
    lv_obj_align(lb_title, LV_ALIGN_TOP_MID, 0, 50);
    lv_obj_add_style(lb_title, &style, 0);
    lv_label_set_text(lb_title, "售卖机管理端");

    lv_obj_t *btn_quit = lv_btn_create(admin_home_page);
    lv_obj_set_size(btn_quit, 100, 50);
    lv_obj_t *lb_quit = lv_label_create(btn_quit);
    lv_obj_add_style(lb_quit, &style, 0);
    lv_obj_center(lb_quit);
    lv_label_set_text(lb_quit, "退出");
    lv_obj_add_event_cb(btn_quit, home_page_quit_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *btn_add = lv_btn_create(admin_home_page);
    lv_obj_set_pos(btn_add, 230, 120);
    lv_obj_set_size(btn_add, 150, 100);
    lv_obj_t *lb_add = lv_label_create(btn_add);
    lv_obj_add_style(lb_add, &style, 0);
    lv_obj_center(lb_add);
    lv_label_set_text(lb_add, "上架新商品");
    lv_obj_add_event_cb(btn_add, add_cb, LV_EVENT_CLICKED, NULL);

    btn_remove = lv_btn_create(admin_home_page);
    lv_obj_align_to(btn_remove, btn_add, LV_ALIGN_OUT_RIGHT_TOP, 40, 0);
    lv_obj_set_size(btn_remove, 150, 100);
    lv_obj_t *lb_remove = lv_label_create(btn_remove);
    lv_obj_add_style(lb_remove, &style, 0);
    lv_obj_center(lb_remove);
    lv_label_set_text(lb_remove, "下架商品");
    lv_obj_add_event_cb(btn_remove, find_cb, LV_EVENT_CLICKED, NULL);

    btn_change = lv_btn_create(admin_home_page);
    lv_obj_align_to(btn_change, btn_add, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20);
    lv_obj_set_size(btn_change, 150, 100);
    lv_obj_t *lb_change = lv_label_create(btn_change);
    lv_obj_add_style(lb_change, &style, 0);
    lv_obj_center(lb_change);
    lv_label_set_text(lb_change, "修改商品");
    lv_obj_add_event_cb(btn_change, find_cb, LV_EVENT_CLICKED, NULL);

    btn_search = lv_btn_create(admin_home_page);
    lv_obj_align_to(btn_search, btn_remove, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20);
    lv_obj_set_size(btn_search, 150, 100);
    lv_obj_t *lb_search = lv_label_create(btn_search);
    lv_obj_add_style(lb_search, &style, 0);
    lv_obj_center(lb_search);
    lv_label_set_text(lb_search, "查询商品");
    lv_obj_add_event_cb(btn_search, find_cb, LV_EVENT_CLICKED, NULL);

    lv_scr_load(admin_home_page);
}

void home_page_quit_cb(lv_event_t *e)
{
    show_outer_page();
    lv_obj_del(admin_home_page);
}

void add_cb(lv_event_t *e)
{
    show_admin_add_page();
}

void find_cb(lv_event_t *e)
{
    lv_obj_t *btn = lv_event_get_current_target(e);
    if(btn == btn_remove)
    {
        ev = REMOVE;
    }
    else if(btn == btn_change)
    {
        ev = CHANGE;
    }
    else if(btn == btn_search)
    {
        ev = SEARCH;
    }
    show_admin_find_page();
}

void show_admin_add_page()
{
    admin_add_page = lv_obj_create(lv_scr_act());
    lv_obj_set_size(admin_add_page, 800, 480);

    lv_obj_add_event_cb(admin_add_page, admin_add_page_hide_kb_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *btn_ret = lv_btn_create(admin_add_page);
    lv_obj_set_size(btn_ret, 100, 50);
    lv_label_t *lb_ret = lv_label_create(btn_ret);
    lv_obj_add_style(lb_ret, &style, 0);
    lv_obj_center(lb_ret);
    lv_label_set_text(lb_ret, "返回");
    lv_obj_add_event_cb(btn_ret, add_ret_home_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *lb_title = lv_label_create(admin_add_page);
    lv_obj_align(lb_title, LV_ALIGN_TOP_MID, 0, 20);
    lv_obj_add_style(lb_title, &style, 0);
    lv_label_set_text(lb_title, "上架新商品");

    lv_obj_t *lb_name = lv_label_create(admin_add_page);
    lv_obj_set_pos(lb_name, 190, 100);
    lv_obj_set_size(lb_name, 100, 50);
    lv_obj_add_style(lb_name, &style, 0);
    lv_label_set_text(lb_name, "名称");
    
    ta_name = lv_textarea_create(admin_add_page);
    lv_obj_align_to(ta_name, lb_name, LV_ALIGN_OUT_RIGHT_TOP, 5, -10);
    lv_obj_set_size(ta_name, 300, 50);
    lv_obj_add_style(ta_name, &style, 0);
    lv_textarea_set_one_line(ta_name, true);
    lv_textarea_set_placeholder_text(ta_name, "请输入新商品名称");
    lv_obj_add_event_cb(ta_name, ta_name_cb, LV_EVENT_CLICKED, NULL);

    lb_quantity = lv_label_create(admin_add_page);
    lv_obj_align_to(lb_quantity, lb_name, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20);
    lv_obj_set_size(lb_quantity, 100, 50);
    lv_obj_add_style(lb_quantity, &style, 0);
    lv_label_set_text(lb_quantity, "剩余数量");
    
    ta_quantity = lv_textarea_create(admin_add_page);
    lv_obj_align_to(ta_quantity, lb_quantity, LV_ALIGN_OUT_RIGHT_TOP, 5, -10);
    lv_obj_set_size(ta_quantity, 300, 50);
    lv_obj_add_style(ta_quantity, &style, 0);
    lv_textarea_set_one_line(ta_quantity, true);
    lv_textarea_set_placeholder_text(ta_quantity, "请输入新商品剩余数量");
    lv_obj_add_event_cb(ta_quantity, ta_quantity_cb, LV_EVENT_CLICKED, NULL);

    lb_price = lv_label_create(admin_add_page);
    lv_obj_align_to(lb_price, lb_quantity, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20);
    lv_obj_set_size(lb_price, 100, 50);
    lv_obj_add_style(lb_price, &style, 0);
    lv_label_set_text(lb_price, "单价");
    
    ta_price = lv_textarea_create(admin_add_page);
    lv_obj_align_to(ta_price, lb_price, LV_ALIGN_OUT_RIGHT_TOP, 5, -10);
    lv_obj_set_size(ta_price, 300, 50);
    lv_obj_add_style(ta_price, &style, 0);
    lv_textarea_set_one_line(ta_price, true);
    lv_textarea_set_placeholder_text(ta_price, "请输入新商品单价");
    lv_obj_add_event_cb(ta_price, ta_price_cb, LV_EVENT_CLICKED, NULL);

    lb_image_name = lv_label_create(admin_add_page);
    lv_obj_align_to(lb_image_name, lb_price, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20);
    lv_obj_set_size(lb_image_name, 100, 50);
    lv_obj_add_style(lb_image_name, &style, 0);
    lv_label_set_text(lb_image_name, "图片名");
    
    ta_image_name = lv_textarea_create(admin_add_page);
    lv_obj_align_to(ta_image_name, lb_image_name, LV_ALIGN_OUT_RIGHT_TOP, 5, -10);
    lv_obj_set_size(ta_image_name, 300, 50);
    lv_obj_add_style(ta_image_name, &style, 0);
    lv_textarea_set_one_line(ta_image_name, true);
    lv_textarea_set_placeholder_text(ta_image_name, "请输入新商品图片名");
    lv_obj_add_event_cb(ta_image_name, ta_image_name_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *btn_confirm_add = lv_btn_create(admin_add_page);
    lv_obj_align(btn_confirm_add, LV_ALIGN_BOTTOM_MID, 0, -10);
    lv_obj_set_size(btn_confirm_add, 100, 50);
    lv_obj_t *lb_confirm_add = lv_label_create(btn_confirm_add);
    lv_obj_add_style(lb_confirm_add, &style, 0);
    lv_obj_center(lb_confirm_add);
    lv_label_set_text(lb_confirm_add, "确定");
    lv_obj_add_event_cb(btn_confirm_add, confirm_add_cb, LV_EVENT_CLICKED, NULL);

    kb = lv_keyboard_create(admin_add_page);
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);

    lv_obj_t *pinyin = lv_ime_pinyin_create(admin_add_page);
    lv_ime_pinyin_set_keyboard(pinyin, kb);
    lv_obj_set_style_text_font(pinyin, info.font, 0);
    lv_ime_pinyin_set_dict(pinyin, my_dict);
}

void admin_add_page_hide_kb_cb(lv_event_t *e)
{
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
    lv_obj_align_to(lb_price, lb_quantity, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20);
    lv_obj_align_to(ta_price, lb_price, LV_ALIGN_OUT_RIGHT_TOP, 5, -10);
    lv_obj_align_to(lb_image_name, lb_price, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20);
    lv_obj_align_to(ta_image_name, lb_image_name, LV_ALIGN_OUT_RIGHT_TOP, 5, -10);
}

void add_ret_home_cb(lv_event_t *e)
{
    lv_scr_load(admin_home_page);
    lv_obj_del(admin_add_page);
}

void ta_name_cb(lv_event_t *e)
{
    lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
    lv_keyboard_set_textarea(kb, ta_name);
}

void ta_quantity_cb(lv_event_t *e)
{
    lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
    lv_keyboard_set_textarea(kb, ta_quantity);
}

void ta_price_cb(lv_event_t *e)
{
    lv_obj_set_pos(lb_price, 190, 20);
    lv_obj_align_to(ta_price, lb_price, LV_ALIGN_OUT_RIGHT_TOP, 5, -10);
    lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
    lv_keyboard_set_textarea(kb, ta_price);
}

void ta_image_name_cb(lv_event_t *e)
{
    lv_obj_set_pos(lb_image_name, 190, 20);
    lv_obj_align_to(ta_image_name, lb_image_name, LV_ALIGN_OUT_RIGHT_TOP, 5, -10);
    lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
    lv_keyboard_set_textarea(kb, ta_image_name);
}

void confirm_add_cb(lv_event_t *e)
{
    char buf[1000] = {0};
    snprintf(buf, sizeof(buf), "%s@%s@%s@S:/project1/images/%s.jpg", lv_textarea_get_text(ta_name), lv_textarea_get_text(ta_quantity), 
            lv_textarea_get_text(ta_price), lv_textarea_get_text(ta_image_name));
    insert_tail(buf);
    add_commodity_info();

    lv_scr_load(admin_home_page);
    lv_obj_del(admin_add_page);
}

void show_admin_find_page()
{
    admin_find_page = lv_obj_create(lv_scr_act());
    lv_obj_set_size(admin_find_page, 800, 480);

    lv_obj_add_event_cb(admin_find_page, admin_page_hide_kb_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *btn_ret = lv_btn_create(admin_find_page);
    lv_obj_set_size(btn_ret, 100, 50);
    lv_label_t *lb_ret = lv_label_create(btn_ret);
    lv_obj_add_style(lb_ret, &style, 0);
    lv_obj_center(lb_ret);
    lv_label_set_text(lb_ret, "返回");
    lv_obj_add_event_cb(btn_ret, find_ret_home_cb, LV_EVENT_CLICKED, NULL);
    
    ta_name = lv_textarea_create(admin_find_page);
    lv_obj_set_pos(ta_name, 200, 100);
    lv_obj_set_size(ta_name, 300, 50);
    lv_obj_add_style(ta_name, &style, 0);
    lv_textarea_set_one_line(ta_name, true);
    lv_textarea_set_placeholder_text(ta_name, "请输入商品名称");
    lv_obj_add_event_cb(ta_name, ta_name_cb, LV_EVENT_CLICKED, NULL);

    list_name = lv_list_create(admin_find_page);
    lv_obj_align_to(list_name, ta_name, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
    lv_obj_set_size(list_name, 300, 200);
    lv_obj_add_style(list_name, &style, 0);
    show_name_list();

    lv_obj_t *btn_find = lv_btn_create(admin_find_page);
    lv_obj_align_to(btn_find, ta_name, LV_ALIGN_OUT_RIGHT_TOP, 20, 0);
    lv_obj_set_size(btn_find, 100, 50);
    lv_obj_t *lb_find = lv_label_create(btn_find);
    lv_obj_add_style(lb_find, &style, 0);
    lv_obj_center(lb_find);
    lv_label_set_text(lb_find, "查找");
    lv_obj_add_event_cb(btn_find, btn_find_cb, LV_EVENT_CLICKED, NULL);

    kb = lv_keyboard_create(admin_find_page);
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);

    lv_obj_t *pinyin = lv_ime_pinyin_create(admin_find_page);
    lv_ime_pinyin_set_keyboard(pinyin, kb);
    lv_obj_set_style_text_font(pinyin, info.font, 0);
    lv_ime_pinyin_set_dict(pinyin, my_dict);
}

void find_ret_home_cb(lv_event_t *e)
{
    lv_scr_load(admin_home_page);
    lv_obj_del(admin_find_page);
}

void show_name_list()
{
    Node *p = head->next;
    while(p != head)
    {
        lv_obj_t *btn = lv_list_add_btn(list_name, NULL, p->name);
        lv_obj_add_event_cb(btn, list_btn_cb, LV_EVENT_CLICKED, NULL);
        p = p->next;
    }
}

void list_btn_cb(lv_event_t *e)
{
    char *str = lv_list_get_btn_text(list_name, lv_event_get_target(e));
    lv_textarea_set_text(ta_name, str);
}

void btn_find_cb(lv_event_t *e)
{
    const char *str = lv_textarea_get_text(ta_name);
    curr_find_node = find_node(str);
    if(curr_find_node != NULL)
    {
        if(ev == REMOVE)
        {
            show_admin_remove_page();
            lv_obj_del(admin_find_page);
        }
        else if(ev == CHANGE)
        {
            show_admin_change_page();
            lv_obj_del(admin_find_page);
        }
        else if(ev == SEARCH)
        {
            show_admin_search_page();
            lv_obj_del(admin_find_page);
        }
    }
    else
    {
        static const char *btns[] = {""};
        lv_obj_t *msgbox = lv_msgbox_create(lv_scr_act(), "提示", "没有找到该商品！", btns, true);
        lv_obj_t *title = lv_msgbox_get_title(msgbox);
        lv_obj_t *text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(title, &style, 0);
        lv_obj_add_style(text, &style, 0);
        lv_obj_center(msgbox);
    }
}

void show_admin_remove_page()
{
    admin_remove_page = lv_obj_create(NULL);
    lv_obj_set_size(admin_remove_page, 800, 480);

    lv_obj_add_event_cb(admin_remove_page, admin_page_hide_kb_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *btn_ret = lv_btn_create(admin_remove_page);
    lv_obj_set_size(btn_ret, 100, 50);
    lv_label_t *lb_ret = lv_label_create(btn_ret);
    lv_obj_add_style(lb_ret, &style, 0);
    lv_obj_center(lb_ret);
    lv_label_set_text(lb_ret, "返回");
    lv_obj_add_event_cb(btn_ret, remove_ret_home_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *lb_title = lv_label_create(admin_remove_page);
    lv_obj_align(lb_title, LV_ALIGN_TOP_MID, 0, 40);
    lv_obj_add_style(lb_title, &style, 0);
    lv_label_set_text(lb_title, "下架商品");

    lv_obj_t *img = lv_img_create(admin_remove_page);
    lv_img_set_src(img, curr_find_node->image_path);
    lv_obj_set_pos(img, 230, 100);
    
    lv_obj_t *lb_name = lv_label_create(admin_remove_page);
    lv_obj_align_to(lb_name, img, LV_ALIGN_OUT_RIGHT_TOP, 50, 20);
    char name[100] = "名称：";
    strncat(name, curr_find_node->name, sizeof(name));
    lv_obj_add_style(lb_name, &style, 0);
    lv_label_set_text(lb_name, name);

    lv_obj_t *lb_quantity = lv_label_create(admin_remove_page);
    lv_obj_align_to(lb_quantity, lb_name, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20);
    char quantity[100] = "剩余数量：";
    char temp_num[50] = {0};
    snprintf(temp_num, sizeof(temp_num), "%ld", curr_find_node->quantity);
    strncat(quantity, temp_num, sizeof(quantity));
    lv_obj_add_style(lb_quantity, &style, 0);
    lv_label_set_text(lb_quantity, quantity);

    lv_obj_t *lb_price = lv_label_create(admin_remove_page);
    lv_obj_align_to(lb_price, lb_quantity, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20);
    char price[100] = "价格：";
    memset(temp_num, 0, sizeof(temp_num));
    snprintf(temp_num, sizeof(temp_num), "%ld", curr_find_node->price);
    strncat(price, temp_num, sizeof(price));
    strncat(price, "元/个", sizeof(price));
    lv_obj_add_style(lb_price, &style, 0);
    lv_label_set_text(lb_price, price);

    lv_obj_t *btn_confirm_remove = lv_btn_create(admin_remove_page);
    lv_obj_align(btn_confirm_remove, LV_ALIGN_BOTTOM_MID, 0, -20);
    lv_obj_set_size(btn_confirm_remove, 150, 50);
    lv_obj_t *lb_confirm_remove = lv_label_create(btn_confirm_remove);
    lv_obj_add_style(lb_confirm_remove, &style, 0);
    lv_obj_center(lb_confirm_remove);
    lv_label_set_text(lb_confirm_remove, "下架该商品");
    lv_obj_add_event_cb(btn_confirm_remove, confirm_remove_cb, LV_EVENT_CLICKED, NULL);

    lv_scr_load(admin_remove_page);
}

void remove_ret_home_cb(lv_event_t *e)
{
    lv_scr_load(admin_home_page);
    lv_obj_del(admin_remove_page);
}

void confirm_remove_cb(lv_event_t *e)
{
    remove_node(curr_find_node);
    overwrite_commodity_info();
    lv_scr_load(admin_home_page);
    lv_obj_del(admin_remove_page);
}

void show_admin_change_page()
{
    admin_change_page = lv_obj_create(lv_scr_act());
    lv_obj_set_size(admin_change_page, 800, 480);

    lv_obj_add_event_cb(admin_change_page, admin_change_page_hide_kb_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *btn_ret = lv_btn_create(admin_change_page);
    lv_obj_set_size(btn_ret, 100, 50);
    lv_label_t *lb_ret = lv_label_create(btn_ret);
    lv_obj_add_style(lb_ret, &style, 0);
    lv_obj_center(lb_ret);
    lv_label_set_text(lb_ret, "返回");
    lv_obj_add_event_cb(btn_ret, change_ret_home_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *lb_title = lv_label_create(admin_change_page);
    lv_obj_align(lb_title, LV_ALIGN_TOP_MID, 0, 20);
    lv_obj_add_style(lb_title, &style, 0);
    lv_label_set_text(lb_title, "修改商品");

    lv_obj_t *lb_name = lv_label_create(admin_change_page);
    lv_obj_set_pos(lb_name, 190, 100);
    lv_obj_set_size(lb_name, 100, 50);
    lv_obj_add_style(lb_name, &style, 0);
    lv_label_set_text(lb_name, "名称");
    
    ta_name = lv_textarea_create(admin_change_page);
    lv_obj_align_to(ta_name, lb_name, LV_ALIGN_OUT_RIGHT_TOP, 5, -10);
    lv_obj_set_size(ta_name, 300, 50);
    lv_obj_add_style(ta_name, &style, 0);
    lv_textarea_set_one_line(ta_name, true);
    lv_textarea_add_text(ta_name, curr_find_node->name);
    lv_obj_add_event_cb(ta_name, ta_name_cb, LV_EVENT_CLICKED, NULL);

    lb_quantity = lv_label_create(admin_change_page);
    lv_obj_align_to(lb_quantity, lb_name, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20);
    lv_obj_set_size(lb_quantity, 100, 50);
    lv_obj_add_style(lb_quantity, &style, 0);
    lv_label_set_text(lb_quantity, "剩余数量");
    
    ta_quantity = lv_textarea_create(admin_change_page);
    lv_obj_align_to(ta_quantity, lb_quantity, LV_ALIGN_OUT_RIGHT_TOP, 5, -10);
    lv_obj_set_size(ta_quantity, 300, 50);
    lv_obj_add_style(ta_quantity, &style, 0);
    lv_textarea_set_one_line(ta_quantity, true);
    char buf[100] = {0};
    snprintf(buf, sizeof(buf), "%ld", curr_find_node->quantity);
    lv_textarea_add_text(ta_quantity, buf);
    lv_obj_add_event_cb(ta_quantity, ta_quantity_cb, LV_EVENT_CLICKED, NULL);

    lb_price = lv_label_create(admin_change_page);
    lv_obj_align_to(lb_price, lb_quantity, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20);
    lv_obj_set_size(lb_price, 100, 50);
    lv_obj_add_style(lb_price, &style, 0);
    lv_label_set_text(lb_price, "单价");
    
    ta_price = lv_textarea_create(admin_change_page);
    lv_obj_align_to(ta_price, lb_price, LV_ALIGN_OUT_RIGHT_TOP, 5, -10);
    lv_obj_set_size(ta_price, 300, 50);
    lv_obj_add_style(ta_price, &style, 0);
    lv_textarea_set_one_line(ta_price, true);
    memset(buf, 0, sizeof(buf));
    snprintf(buf, sizeof(buf), "%ld", curr_find_node->price);
    lv_textarea_add_text(ta_price, buf);
    lv_obj_add_event_cb(ta_price, ta_price_cb, LV_EVENT_CLICKED, NULL);

    lb_image_name = lv_label_create(admin_change_page);
    lv_obj_align_to(lb_image_name, lb_price, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20);
    lv_obj_set_size(lb_image_name, 100, 50);
    lv_obj_add_style(lb_image_name, &style, 0);
    lv_label_set_text(lb_image_name, "图片名");
    
    ta_image_name = lv_textarea_create(admin_change_page);
    lv_obj_align_to(ta_image_name, lb_image_name, LV_ALIGN_OUT_RIGHT_TOP, 5, -10);
    lv_obj_set_size(ta_image_name, 300, 50);
    lv_obj_add_style(ta_image_name, &style, 0);
    lv_textarea_set_one_line(ta_image_name, true);
    memset(buf, 0, sizeof(buf));
    strncpy(buf, curr_find_node->image_path, sizeof(buf));
    char *p = strtok(buf, "/");
    while(strstr(p, ".jpg") == NULL)
    {
        p = strtok(NULL, "/");
    }
    p = strtok(p, ".");
    lv_textarea_add_text(ta_image_name, p);
    lv_obj_add_event_cb(ta_image_name, ta_image_name_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *btn_confirm_change = lv_btn_create(admin_change_page);
    lv_obj_align(btn_confirm_change, LV_ALIGN_BOTTOM_MID, 0, -10);
    lv_obj_set_size(btn_confirm_change, 100, 50);
    lv_obj_t *lb_confirm_change = lv_label_create(btn_confirm_change);
    lv_obj_add_style(lb_confirm_change, &style, 0);
    lv_obj_center(lb_confirm_change);
    lv_label_set_text(lb_confirm_change, "确定");
    lv_obj_add_event_cb(btn_confirm_change, confirm_change_cb, LV_EVENT_CLICKED, NULL);

    kb = lv_keyboard_create(admin_change_page);
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);

    lv_obj_t *pinyin = lv_ime_pinyin_create(admin_change_page);
    lv_ime_pinyin_set_keyboard(pinyin, kb);
    lv_obj_set_style_text_font(pinyin, info.font, 0);
    lv_ime_pinyin_set_dict(pinyin, my_dict);
}

void admin_change_page_hide_kb_cb(lv_event_t *e)
{
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
    lv_obj_align_to(lb_price, lb_quantity, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20);
    lv_obj_align_to(ta_price, lb_price, LV_ALIGN_OUT_RIGHT_TOP, 5, -10);
    lv_obj_align_to(lb_image_name, lb_price, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20);
    lv_obj_align_to(ta_image_name, lb_image_name, LV_ALIGN_OUT_RIGHT_TOP, 5, -10);
}

void change_ret_home_cb(lv_event_t *e)
{
    lv_scr_load(admin_home_page);
    lv_obj_del(admin_change_page);
}

void confirm_change_cb(lv_event_t *e)
{
    strncpy(curr_find_node->name, lv_textarea_get_text(ta_name), sizeof(curr_find_node->name));
    curr_find_node->quantity = strtol(lv_textarea_get_text(ta_quantity), NULL, 10);
    curr_find_node->price = strtol(lv_textarea_get_text(ta_price), NULL, 10);
    char buf[100] = {0};
    snprintf(buf, sizeof(buf), "S:/project1/images/%s.jpg", lv_textarea_get_text(ta_image_name));
    strncpy(curr_find_node->image_path, buf, sizeof(curr_find_node->image_path));
    overwrite_commodity_info();
    lv_scr_load(admin_home_page);
    lv_obj_del(admin_change_page);
}

void show_admin_search_page()
{
    admin_search_page = lv_obj_create(NULL);
    lv_obj_set_size(admin_search_page, 800, 480);

    lv_obj_t *btn_ret = lv_btn_create(admin_search_page);
    lv_obj_set_size(btn_ret, 100, 50);
    lv_label_t *lb_ret = lv_label_create(btn_ret);
    lv_obj_add_style(lb_ret, &style, 0);
    lv_obj_center(lb_ret);
    lv_label_set_text(lb_ret, "返回");
    lv_obj_add_event_cb(btn_ret, search_ret_home_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *img = lv_img_create(admin_search_page);
    lv_img_set_src(img, curr_find_node->image_path);
    lv_obj_set_pos(img, 230, 60);
    
    lv_obj_t *lb_name = lv_label_create(admin_search_page);
    lv_obj_align_to(lb_name, img, LV_ALIGN_OUT_RIGHT_TOP, 50, 20);
    char name[100] = "名称：";
    strncat(name, curr_find_node->name, sizeof(name));
    lv_obj_add_style(lb_name, &style, 0);
    lv_label_set_text(lb_name, name);

    lv_obj_t *lb_quantity = lv_label_create(admin_search_page);
    lv_obj_align_to(lb_quantity, lb_name, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20);
    char quantity[100] = "剩余数量：";
    char temp_num[50] = {0};
    snprintf(temp_num, sizeof(temp_num), "%ld", curr_find_node->quantity);
    strncat(quantity, temp_num, sizeof(quantity));
    lv_obj_add_style(lb_quantity, &style, 0);
    lv_label_set_text(lb_quantity, quantity);

    lv_obj_t *lb_price = lv_label_create(admin_search_page);
    lv_obj_align_to(lb_price, lb_quantity, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20);
    char price[100] = "价格：";
    memset(temp_num, 0, sizeof(temp_num));
    snprintf(temp_num, sizeof(temp_num), "%ld", curr_find_node->price);
    strncat(price, temp_num, sizeof(price));
    strncat(price, "元/个", sizeof(price));
    lv_obj_add_style(lb_price, &style, 0);
    lv_label_set_text(lb_price, price);

    lv_scr_load(admin_search_page);
}

void search_ret_home_cb(lv_event_t *e)
{
    lv_scr_load(admin_home_page);
    lv_obj_del(admin_search_page);
}